package cn.cqie.frame.service.impl;

import cn.cqie.frame.bo.query.QueryEvaluateTypeBo;
import cn.cqie.frame.entity.Clazz;
import cn.cqie.frame.entity.Course;
import cn.cqie.frame.entity.Dept;
import cn.cqie.frame.entity.EvaluateType;
import cn.cqie.frame.mapper.EvaluateTypeMapper;
import cn.cqie.frame.service.EvaluateTypeService;
import cn.cqie.frame.utils.AssertUtil;
import cn.cqie.frame.utils.BeanCopyUtils;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.CourseVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评价类型 服务实现类
 * </p>
 *
 * @author XYHL
 * @since 2021-10-12
 */
@Service
public class EvaluateTypeServiceImpl extends ServiceImpl<EvaluateTypeMapper, EvaluateType> implements EvaluateTypeService {
    @Autowired
    private EvaluateTypeService evaluateTypeService;

    /**
     * 插入或更新评价信息
     * @param bo
     * @return
     */
    @Override
    public EvaluateType saveOrUpdateEvaluateType(EvaluateType bo) {
        EvaluateType evaluateType = BeanCopyUtils.copyObject(bo,EvaluateType.class);
        LocalDateTime sysTime = LocalDateTime.now();
        evaluateType.setCreateTime(sysTime);
        evaluateType.setUpdateTime(sysTime);
        boolean insert = evaluateTypeService.saveOrUpdate(evaluateType);
        AssertUtil.isTrue(insert, "插入/更新菜单失败");
        return BeanUtil.toBean(evaluateType, EvaluateType.class);
    }

    @Override
    public Boolean delete(Integer evaluateTypeId) {
        boolean b = evaluateTypeService.removeById(evaluateTypeId);
        return b;
    }

    /**
     *
     * @param bo
     * @return
     */
    @Override
    public List<EvaluateType> listEvaluateType(QueryEvaluateTypeBo bo) {
        MyPageHelper.startPage(bo);
        LambdaQueryWrapper<EvaluateType> lqw = new LambdaQueryWrapper<>();
        lqw.like(bo.getKeywords()!=null,EvaluateType::getName,bo.getKeywords());
        List<EvaluateType> courseList = baseMapper.selectList(lqw);
        return entity2Vo(courseList);
    }

    /**
     *
     * @param collection
     * @return
     */
    private List<EvaluateType> entity2Vo(List<EvaluateType> collection) {
        List<EvaluateType> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, EvaluateType.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<EvaluateType> page = (Page<EvaluateType>) collection;
            Page<EvaluateType> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }
}
